//*************************************************************************************************************
var ANSWER_COUNT_LIMITATION = 7 ;

function addAnswerRow(id){
	
	var oldValue = $('#answercount').val();
	var newVal = parseFloat(oldValue) + 1;
	
	var dataRow = '<div id="answerrow"'+ newVal +'>';
		dataRow += '<div class="row">';
		dataRow += '   	<div class="col-md-2">';
		dataRow += '   		<h4 class="list-group-item-heading">Answer'+ newVal +'</h4>';
		dataRow += '   	</div>';
		dataRow += '	<div class="col-md-7">';
		dataRow += '		<input type="text" class="form-control" id="answercontent'+ newVal +'">';
		dataRow += '	</div>';
		dataRow += '	<div class="col-md-2">';
		dataRow += '    	<div class="input-group">';
		dataRow += '	  	<span class="form-control" >Correct</span>';
		dataRow += '      	<span class="input-group-addon">';
		dataRow += '        	<input type="checkbox" aria-label="..." id="checkcorrect'+ newVal +'" >';
		dataRow += '      	</span>';
		dataRow += '    	</div><!-- /input-group -->';
		dataRow += '	</div><!-- /.col-lg-3 -->';
		dataRow += '	<div class="col-md-1">';
		dataRow += '		<button type="button" class="btn btn-default btn-lg" onclick="removeme(this)">';
		dataRow += '			<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>';
		dataRow += '		</button>';
		dataRow += '	</div>';
		dataRow += ' </div>';
		dataRow += '</div>';
		
	
		/*	 	  var obj = JSON.parse(JSON.stringify(jsonp.items));
	 	  var buttonval = '<td><span aria-hidden="true" class="margin glyphicon glyphicon-remove-circle"></span>';
	 	  $.each(obj, function() {
	 		 dataRow += "<tr><td>" + this['id'] + "</td><td>" + this['categoryName'] + "</td><td>" + this['parentId'] + "<td/>" + buttonval + "</tr>";
	 	  });
	 	    dataRow +=  '</tbody>';
	 	    dataRow +='</table>';
	 	    */
		if (newVal < ANSWER_COUNT_LIMITATION){
	 	 $('answerrow').append(dataRow);
	 	 $('#answercount').val(newVal);
		}
		else 
			alert('You are not allowed to add more than 6 rows.');
}
//*************************************************************************************************************
function addQuestion(data){
	 var request = gapi.client.api.sham.amozesh.question.insert({
	 'level':data.level,
	 'qType':data.qType,
	 'qExperssion':data.qExperssion,
	 'id':null,
	 'answers': data.answers
	 });
	 request.execute(function(jsonResp, rawResp) { 
		 console.log('test');
    });
}
/*
function insertBusiness(datain){
	var category = {categoryId:"222", categoryName:"Doe",  parentId:"333"};
	var person = {businessName:"2",firstName:"2",lastName:"3",personId:"100"};
	var contact = {address1:"A",address2:"B",city:"C",contactId:"300",country:"E",latitude:"11.25",longitude:"22.35",phones:"12345",postalCode:"123",province:"BC"};
	var description = "a";
	var imageUrl = "b";
	var type= "private";
	var tag=['biz'];
	var rating = "4";
	var data = {category:category,person:person,contact:contact,description:description,imageUrl:imageUrl,type:type,tag:tag,rating:rating};
	answers: [{aExperssion: {value: "abc"},isTrue: true},{aExperssion: {value: "bcv"},isTrue: false},{aExperssion: {value: "efr"},isTrue: false}
 ],
	addABusiness(data);
}
*/
function insertBusiness(datain){
	var category = {categoryId:datain.categoryId, categoryName:datain.categoryName,  parentId:datain.parentId};
	var owner = {businessName:datain.businessName,firstName:datain.firstName,lastName:datain.lastName,personId:datain.personId};
	var contact = {address1:datain.address1,address2:datain.address2,city:datain.city,contactId:datain.contactId,country:datain.country,
			latitude:datain.latitude,longitude:datain.longitude,phones:datain.phones,postalCode:datain.postalCode,province:datain.province};
	var description = datain.description;
	var imageUrl = datain.imageUrl;
	var type= datain.type;
	var tag= [ "'" + datain.tag + "'" ];
	var rating = datain.rating;
	
	var data = {category:category,owner:owner,contact:contact,description:description,imageUrl:imageUrl,type:type,tag:tag,rating:rating};
	addABusiness(data);
}

//*************************************************************************************************************
function getquestion(key){
	 var request = gapi.client.api.sham.amozesh.question.getquestion({
	 'id':key
	 });
	 request.execute(function(jsonResp, rawResp) { 
		 renderBusinessByTag(jsonResp);
   });
}

function createJson(){
	var loopcounter = $("#answercount").val();
	var answers ='[';
	for (i = 1; i <= loopcounter; ++i) {
		answers += '{"aExperssion": {"value":"'+ $("#answercontent"+i).val() +'"}, "isTrue":'+ $("#checkcorrect" + i).prop('checked') +'},'
	}
	answers = answers.substring(0, answers.length - 1);
	answers += ']' ;
	var retObj = jQuery.parseJSON(answers);
	return retObj;
}

function insertAQuestion(){
	 var level = 4; //means hard //$("#answercount").val();
	 var qType = 'THREEOPTIONS' ;
	 var qExperssion = '{"value":'+ $("#questionBody").val() +'}';
	 var id = null ;
	 var answers = createJson();
	 var data = {level:level,qType:qType,qExperssion:qExperssion,answers:answers,id:id};
	 addQuestion(data); 
    console.log(data);
}

